/*
 * WiiRemoteManager.java
 *
 *  Copyright (c) 2008 Corey Oliver and Anshul Kumaria
 *
 *  This file is part of Balls of Fury.
 *
 *  Balls of Fury is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Balls of Fury is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 *
 */

/*
 * EDIT:  03/13/2008 - This is an example of how an edit performed to the class 
 * would appear.
 */
package ballsoffury.input;

import java.util.ArrayList;
import wiiremotej.WiiRemote;
import wiiremotej.event.WRIREvent;
import wiiremotej.ButtonMap;
import wiiremotej.event.WRButtonEvent;
import wiiremotej.event.WRNunchukExtensionEvent;

/**
 * Allows for management of the wiimotes. The class allows the client to add, 
 * remove, modify leds, and attach controllers to wiimotes.
 *
 * @author Corey Oliver
 * @version $Revision:$ $Date:$
 */
public class WiiRemoteManager {

    private static final boolean[] PLAYER1_LED =
            new boolean[]{true, false, false, false};
    private static final boolean[] PLAYER2_LED =
            new boolean[]{true, true, false, false};
    private static final boolean[] PLAYER3_LED =
            new boolean[]{true, true, true, false};
    private static final boolean[] PLAYER4_LED =
            new boolean[]{true, true, true, true};
    private static ArrayList<WiiRemote> wiiRemotes =
            new ArrayList();
    private static WiiRemoteDiscoverer wiiRemoteDiscoverer =
            new WiiRemoteDiscoverer();
    private static int maxPlayers = 7;

    /**
     *
     * <code>addWiiRemote</code> checks if <code>maxPlayers</code> has been
     * surpassed, and if not adds a <code>WiiRemote</code> to the
     * <code>WiiRemoteManager</code> <code>wiiRemotes</code> 
     * <code>ArrayList</code> and sets certain features of the 
     * <code>WiiRemote</code> specified by the client.
     * @param wiiRemote the <code>WiiRemote</code> to be added to the
     * <code>wiiRemotes</code> <code>ArrayList</code>.
     * @param accelerometerEnabled this determines whether the accelerometer
     * is enabled for <code>wiiRemote</code>.
     * @param speakerEnabled this determines whether the speaker
     * is enabled for <code>wiiRemote</code>.
     * @param irSensorEnabled this determines whether the IR sensor
     * is enabled for <code>wiiRemote</code>.
     */
    public static void addWiiRemote(WiiRemote remote, WiiController wiiRemoteController,
            boolean accelerometerEnabled, boolean speakerEnabled,
            boolean irSensorEnabled) {
        System.out.println(wiiRemotes.size());
        if (!isMaxPlayersSurpassed()) {
            wiiRemotes.add(remote);
            wiiRemoteController.attachWiiRemote(remote);
            remote.addWiiRemoteListener(wiiRemoteController);
            try {
                remote.setAccelerometerEnabled(accelerometerEnabled);
                remote.setSpeakerEnabled(speakerEnabled);
                remote.setIRSensorEnabled(irSensorEnabled, WRIREvent.BASIC);
            } catch (Exception e) {
                e.printStackTrace();
            }
            remote.getButtonMaps().add(new ButtonMap(WRButtonEvent.HOME,
                    ButtonMap.NUNCHUK, WRNunchukExtensionEvent.C,
                    new int[]{java.awt.event.KeyEvent.VK_CONTROL},
                    java.awt.event.InputEvent.BUTTON1_MASK, 0, -1));
            setLEDLights(remote);
        }
    }

    /**
     *
     * <code>addWiiRemote</code> checks if <code>maxPlayers</code> has been
     * surpassed, and if not adds a <code>WiiRemote</code> to the
     * <code>WiiRemoteManager</code> <code>wiiRemotes</code> 
     * <code>ArrayList</code> gives it default settings.
     * @param wiiRemote the <code>WiiRemote</code> to be added to the
     * <code>wiiRemotes</code> <code>ArrayList</code>.
     */
    public static void addWiiRemote(WiiRemote remote, WiiController wiiRemoteController) {
        addWiiRemote(remote, wiiRemoteController, true, true, true);
        }

    public static void removeWiiRemote(WiiRemote remote) {
        wiiRemotes.remove(remote);
    }

    public static void setRemoteController(WiiRemote remote) {
        
    }
    public static WiiRemote getWiiRemote(int playerNumber) {
        return wiiRemotes.get(playerNumber - 1);
    }

    public static void setLEDLights(WiiRemote remote, boolean[] lightStatus) {
        try {
            remote.setLEDLights(lightStatus);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void setLEDLights(WiiRemote remote) {
        try {
            switch (wiiRemotes.size()) {
                case 1:
                    remote.setLEDLights(PLAYER1_LED);
                    break;
                case 2:
                    remote.setLEDLights(PLAYER2_LED);
                    break;
                case 3:
                    remote.setLEDLights(PLAYER3_LED);
                    break;
                case 4:
                    remote.setLEDLights(PLAYER4_LED);
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void setMaxPlayers(int max) {
        maxPlayers = max;
    }

    private static boolean isMaxPlayersSurpassed() {
        return (wiiRemotes.size() == maxPlayers);
    }

    public static void discoverWiiRemotes(WiiController wiiRemoteController) {
        wiiRemoteDiscoverer.discoverWiiRemotes(wiiRemoteController);
    }

    public static void stopDiscoverWiiRemotes() {
        wiiRemoteDiscoverer.stopDiscoverWiiRemotes();
    }
    
    public static void disconnectAllWiiRemotes() {
        for (WiiRemote remote : wiiRemotes) {
            removeWiiRemote(remote);
        }
        wiiRemotes.clear();
    }
}
